package com.example.yb.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.util.GetTokenUtil;
import com.example.util.HttpClientUtils;
import com.example.util.JwdUtil;
import com.example.yb.entity.*;
import com.example.yb.mapper.YB_1w1mMapper;
import com.example.yb.mapper.YB_1w1m_tbMapper;
import com.example.yb.mapper.YB_ssbMapper;
import com.example.yb.service.YBService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
<<<<<<< HEAD
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
=======
>>>>>>> 3f039bc935edf091c1f1437e0c1ddcf82f9a410e
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author
 * @createtime 2021-08-14 9:27
 * @description
 */
@Service
@Component
public class YBServiceImpl implements YBService {

    @Autowired
    private YB_1w1m_tbMapper yb_1w1m_tbMapper;

    @Autowired
    private YB_ssbMapper yb_ssbMapper;

    @Autowired
    private YB_1w1mMapper yb_1w1mMapper;


    @Autowired
    private GetTokenUtil getTokenUtil;

    @Override
    public ArrayList<Iyqww_yb_wbssb_1w1mtb> findAddress() {
        return yb_1w1m_tbMapper.findAddress();
    }

    @Override
    public ArrayList<Iyqww_yb_wbssb_ssb> findAddress1() {
        return yb_ssbMapper.findAddress();
    }

    @Override
    public ArrayList<Iyqww_yb_wbssb_1w1m> findAddress2() {
        return yb_1w1mMapper.findAddress();
    }


    @Override
    public void addForeignId(String str, String address) {
        yb_1w1m_tbMapper.addForeignId(str, address);

    }

    @Override
    public void addForeignId1(String str, String address) {
        yb_ssbMapper.addForeignId(str, address);

    }


    @Override
    public void addForeignId_val(String foreignId_val, String address) {
        yb_1w1mMapper.addForeignId_val(foreignId_val, address);
        System.out.println(foreignId_val);
        System.out.println(address);

    }


    @Override
    public ArrayList<Map<String, Object>> findEssential() {

        ArrayList<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();

        List<Iyqww_yb_wbssb_1w1mtb> list = yb_1w1m_tbMapper.findEssential();
        //查询随手报中的foreignId
        ArrayList<Iyqww_yb_wbssb_ssb> ssb_list = yb_ssbMapper.findAddress();
        // regEEx-- String规则
        String regEx = "[\n`~!@#$%^&*()+=|';'\\[\\].<>/?~！@#￥%……&*（）——+|【】‘；：”“’。 、？]";
        // 把规则编译成模式对象
        Pattern p = Pattern.compile(regEx);

        String deptName = null;
        String userName = null;
        String mobile = null;
        String aa = "";
        // url 根据用户id获取用户信息 云枢接口
        String url = "http://acl.iansn.com/api/api/organization/user/info";
        // dept_url 根据部门id获取部门信息 云枢接口
        String dept_url = "http://acl.iansn.com/api/api/organization/department/get_dept_info";
        try {
            String token = getTokenUtil.getToken();

            for (Iyqww_yb_wbssb_1w1mtb iy : list) {

                //点位基础信息pointInformationMap
                Map<String, Object> pointInformationMap = new HashMap<>();
                //获取地址
                String address = iy.getAddress();
                //将地址转为经纬度
                String addressStr = JwdUtil.getLonLat1(address);
//                System.out.println(addressStr);
                //获取1w1m_tb的foreignId
                String foreignId = iy.getForeignId();

                //获取部门id
                String StaffSelector1608775420787 = iy.getStaffSelector1608775420787();

                if (StaffSelector1608775420787 != null && StaffSelector1608775420787 != "") {

                    // 通过模式对象得到匹配器对象
                    Matcher ma = p.matcher(StaffSelector1608775420787);
                    String newStr = ma.replaceAll(aa).trim();
                    //将处理后的字符串放到map中
                    Map map = (Map) JSON.parse(newStr);
                    //从map中取得id的值
                    String deptId = (String) map.get("id");
                    //根据部门ID获取部门信息
                    String dept = HttpClientUtils.doGetDept(dept_url, token, deptId);
                    //处理得到的部门信息
                    JSONObject deptJson = JSONObject.fromObject(dept);
                    //从部门信息中获取data的值
                    String dataStr = deptJson.getString("data");
                    //处理得到的data值
                    JSONObject deptJson1 = JSONObject.fromObject(dataStr);
                    //从data值中获取到部门名称deptName
                    deptName = deptJson1.getString("name");

                }


//                System.out.println("deptName============" + deptName);


                //获取用户id
                String staffSelector1608775349969 = iy.getStaffSelector1608775349969();

                if (staffSelector1608775349969 != null && staffSelector1608775349969 != "") {
                    Matcher m = p.matcher(staffSelector1608775349969);
                    String newString = m.replaceAll(aa).trim();
                    Map maps = (Map) JSON.parse(newString);
                    String userId = (String) maps.get("id");
                    //根据用户ID获取姓名、电话
                    String inforSt = HttpClientUtils.doGetUser(url, token, userId);
                    JSONObject userJson = JSONObject.fromObject(inforSt);
                    String user_data = userJson.getString("data");
                    JSONObject userJson1 = JSONObject.fromObject(user_data);
                    userName = userJson1.getString("name");
                    mobile = userJson1.getString("mobile");
                }


//                System.out.println("userName============" + userName);
////                System.out.println("mobile============" + mobile);

                pointInformationMap.put("userName", userName);
                pointInformationMap.put("mobile", mobile);
                pointInformationMap.put("address", address);
                pointInformationMap.put("addressJwd", addressStr);
                pointInformationMap.put("foreignId", foreignId);
                pointInformationMap.put("deptName", deptName);

                //循环ssb_list得到ssbForeignId
                for (Iyqww_yb_wbssb_ssb iy_ssb : ssb_list) {
                    String ssbForeignId = iy_ssb.getForeignId();

                    //判断随手报的foreignId是否与1w1m_tb中的foreignId相同
                    if (ssbForeignId != null && ssbForeignId != "" && ssbForeignId.equals(foreignId)) {
                        //相同则查询办结情况
                        List<Map<String, Object>> ssbList = yb_ssbMapper.staticData(foreignId);

                        for (Map<String, Object> map1 : ssbList) {

                            if (map1.get("ShortText1608803337156").equals("已办结")) {
                                Object num = map1.get("num");

                                pointInformationMap.put("completed", num);
//                                System.out.println("已办结数量==="+num);
                            } else {
                                Object num = map1.get("num");
                                pointInformationMap.put("processing", num);
                            }

                        }
                    } else {//随手报的foreignId是否与1w1m_tb中的foreignId不相同时直接赋值

                        pointInformationMap.put("completed", 0);
                        pointInformationMap.put("processing", 0);
                    }

                }

                System.out.println("tb_1w1mMap====" + pointInformationMap);
                list1.add(pointInformationMap);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list1;


    }


    @Override
    public List<Iyqww_yb_wbssb_ssbVo> queryAllSsbVo(Page<Iyqww_yb_wbssb_ssbVo> pagination, String foreignId) {
        //查数据库获取数据
        List<Iyqww_yb_wbssb_ssbVo> ssbxtLists = yb_ssbMapper.queryAllSsbVo(pagination, foreignId);

        List<Iyqww_yb_wbssb_ssbVo> ssbxtList = new ArrayList<>();


        for (Iyqww_yb_wbssb_ssbVo ssbxtVo : ssbxtLists) {

            Iyqww_yb_wbssb_ssbVo ssbxtVo1 = new Iyqww_yb_wbssb_ssbVo();

            String problemsSummary = "";

            String str1 = "";


            String shortText1608777161506 = ssbxtVo.getShortText1608777161506();
            String shortText1608796211477 = ssbxtVo.getShortText1608796211477();
            String shortText1608799766004 = ssbxtVo.getShortText1608799766004();
            String shortText1608801262580 = ssbxtVo.getShortText1608801262580();
            String shortText1608801569614 = ssbxtVo.getShortText1608801569614();
            String shortText1608801859045 = ssbxtVo.getShortText1608801859045();

            String str = shortText1608777161506 + "," + shortText1608796211477 + "," +
                    shortText1608799766004 + "," + shortText1608801262580 + "," +
                    shortText1608801569614 + "," + shortText1608801859045;

            str1 = StrUtil.removeAll(str, "null,");
            if (str1.endsWith(",")) {
                //字符串如果以“，”结尾则去掉字符的最后一个字符
                problemsSummary = str1.substring(0, str1.length() - 1);

                System.out.println("sfsfsfklafks" + problemsSummary);

            } else if (str1.endsWith(",null")) {
                String str2 = StrUtil.removeAll(str1, ",");
                problemsSummary = StrUtil.removeAll(str2, "null");
            } else if (str1.startsWith(",")) {

                problemsSummary = StrUtil.removeAll(str1, ",");

            } else {
                problemsSummary = str1;
            }

            ssbxtVo1.setProblemsSummary(problemsSummary);

            ssbxtVo1.setAddress(ssbxtVo.getAddress());
            ssbxtVo1.setForeignId(ssbxtVo.getForeignId());
            ssbxtVo1.setSequenceStatus(ssbxtVo.getSequenceStatus());
            ssbxtVo1.setShortText1608803337156(ssbxtVo.getShortText1608803337156());

            ssbxtVo1.setShortText1608772055540(ssbxtVo.getShortText1608772055540());
            ssbxtVo1.setShortText1608772160647(ssbxtVo.getShortText1608772160647());

            ssbxtVo1.setStartTime(ssbxtVo.getStartTime());
            ssbxtVo1.setFinishTime(ssbxtVo.getFinishTime());

            ssbxtList.add(ssbxtVo1);


        }


        return ssbxtList;
    }

    @Override
    public List<Iyqww_yb_wbssb_ssbVo> queryAllSsbVo1(Page<Iyqww_yb_wbssb_ssbVo> pagination, String foreignId) {
        //查数据库获取数据
        List<Iyqww_yb_wbssb_ssbVo> ssbxtLists = yb_ssbMapper.queryAllSsbVo1(pagination, foreignId);

        List<Iyqww_yb_wbssb_ssbVo> ssbxtList = new ArrayList<>();


        for (Iyqww_yb_wbssb_ssbVo ssbxtVo : ssbxtLists) {

            Iyqww_yb_wbssb_ssbVo ssbxtVo1 = new Iyqww_yb_wbssb_ssbVo();

            String problemsSummary = "";

            String str1 = "";


            String shortText1608777161506 = ssbxtVo.getShortText1608777161506();
            String shortText1608796211477 = ssbxtVo.getShortText1608796211477();
            String shortText1608799766004 = ssbxtVo.getShortText1608799766004();
            String shortText1608801262580 = ssbxtVo.getShortText1608801262580();
            String shortText1608801569614 = ssbxtVo.getShortText1608801569614();
            String shortText1608801859045 = ssbxtVo.getShortText1608801859045();

            String str = shortText1608777161506 + "," + shortText1608796211477 + "," +
                    shortText1608799766004 + "," + shortText1608801262580 + "," +
                    shortText1608801569614 + "," + shortText1608801859045;

            str1 = StrUtil.removeAll(str, "null,");


            if (str1.endsWith(",")) {
                //字符串如果以“，”结尾则去掉字符的最后一个字符
                problemsSummary = str1.substring(0, str1.length() - 1);

                System.out.println("sfsfsfklafks" + problemsSummary);

            } else if (str1.endsWith(",null")) {
                String str2 = StrUtil.removeAll(str1, ",");
                problemsSummary = StrUtil.removeAll(str2, "null");
            } else if (str1.startsWith(",")) {

                problemsSummary = StrUtil.removeAll(str1, ",");

            } else {
                problemsSummary = str1;
            }

            System.out.println("problemsSummary====" + problemsSummary);
            ssbxtVo1.setProblemsSummary(problemsSummary);
            ssbxtVo1.setActivityName(ssbxtVo.getActivityName());
            ssbxtVo1.setAddress(ssbxtVo.getAddress());
            ssbxtVo1.setForeignId(ssbxtVo.getForeignId());
            ssbxtVo1.setSequenceStatus(ssbxtVo.getSequenceStatus());
            ssbxtVo1.setShortText1608803337156(ssbxtVo.getShortText1608803337156());
            ssbxtVo1.setParticipantName(ssbxtVo.getParticipantName());
            ssbxtVo1.setReceiveTime(ssbxtVo.getReceiveTime());
            ssbxtVo1.setShortText1608772055540(ssbxtVo.getShortText1608772055540());
            ssbxtVo1.setShortText1608772160647(ssbxtVo.getShortText1608772160647());
            ssbxtList.add(ssbxtVo1);


        }


        return ssbxtList;
    }


    /**
     * 缓存表插入数据
     */

    public void setssbVoinfo() {

        Iyqww_yb_wbssb_ssbVo ssbVo = new Iyqww_yb_wbssb_ssbVo();

        List<Iyqww_yb_wbssb_1w1mtb> list = yb_1w1m_tbMapper.findEssential();
        //查询随手报中的foreignId
        ArrayList<Iyqww_yb_wbssb_ssb> ssb_list = yb_ssbMapper.findAddress();
        // regEEx-- String规则
        String regEx = "[\n`~!@#$%^&*()+=|';'\\[\\].<>/?~！@#￥%……&*（）——+|【】‘；：”“’。 、？]";
        // 把规则编译成模式对象
        Pattern p = Pattern.compile(regEx);


        String aa = "";
        // url 根据用户id获取用户信息 云枢接口
        String url = "http://acl.iansn.com/api/api/organization/user/info";
        // dept_url 根据部门id获取部门信息 云枢接口
        String dept_url = "http://acl.iansn.com/api/api/organization/department/get_dept_info";
        try {
            String token = getTokenUtil.getToken();

            Iyqww_yb_wbssb_1w1mtb iy;
//            for (Iyqww_yb_wbssb_1w1mtb iy : list) {

            for (int i = 0; i < list.size(); i++) {
                iy = list.get(i);
//                System.out.println(iy);
                //点位基础信息pointInformationMap
                Map<String, Object> pointInformationMap = new HashMap<>();
                String deptName = null;
                String userName = null;
                String mobile = null;
                    String newAddress = "大理市漾濞县";

                //获取单据状态
                String sequenceStatus = iy.getSequenceStatus();

                //获取地址
                String address = iy.getAddress();
                newAddress += address;


                //将地址转为经纬度
                String addressStr = String.valueOf(JwdUtil.getLonLat1(newAddress));

                //获取1w1m_tb的foreignId
                String foreignId = iy.getForeignId();

                //获取部门id
                String StaffSelector1608775420787 = iy.getStaffSelector1608775420787();

                if (StaffSelector1608775420787 != null && StaffSelector1608775420787 != "") {

                    // 通过模式对象得到匹配器对象
                    Matcher ma = p.matcher(StaffSelector1608775420787);
                    String newStr = ma.replaceAll(aa).trim();
                    //将处理后的字符串放到map中
                    Map map = (Map) JSON.parse(newStr);
                    //从map中取得id的值
                    String deptId = (String) map.get("id");
                    //根据部门ID获取部门信息
                    String dept = HttpClientUtils.doGetDept(dept_url, token, deptId);
                    //处理得到的部门信息
                    JSONObject deptJson = JSONObject.fromObject(dept);
                    //从部门信息中获取data的值
                    String dataStr = deptJson.getString("data");
                    //处理得到的data值
                    JSONObject deptJson1 = JSONObject.fromObject(dataStr);
                    //从data值中获取到部门名称deptName
                    deptName = deptJson1.getString("name");

                }

                //获取用户id
                String staffSelector1608775349969 = iy.getStaffSelector1608775349969();

                if (staffSelector1608775349969 != null && staffSelector1608775349969 != "") {
                    Matcher m = p.matcher(staffSelector1608775349969);
                    String newString = m.replaceAll(aa).trim();
                    Map maps = (Map) JSON.parse(newString);
                    String userId = (String) maps.get("id");
                    //根据用户ID获取姓名、电话
                    String inforSt = HttpClientUtils.doGetUser(url, token, userId);
                    JSONObject userJson = JSONObject.fromObject(inforSt);
                    String user_data = userJson.getString("data");
                    JSONObject userJson1 = JSONObject.fromObject(user_data);
                    userName = userJson1.getString("name");
                    mobile = userJson1.getString("mobile");
                }

//                System.out.println("deptName=====" + deptName);
                ssbVo.setUserName(userName);
                ssbVo.setMobile(mobile);
                ssbVo.setAddress(address);
                ssbVo.setAddressJwd(addressStr);
                ssbVo.setForeignId(foreignId);
                ssbVo.setDeptName(deptName);
                ssbVo.setSequenceStatus(sequenceStatus);


                yb_ssbMapper.add_ssbVo(ssbVo);
                System.out.println("ssbVo===" + ssbVo);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public ArrayList<Map<String, Object>> selectAll() {

        ArrayList<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();

        List<Iyqww_yb_wbssb_ssbVo> ssbVoList = yb_ssbMapper.selectAll();
        //查询随手报中的foreignId
        ArrayList<Iyqww_yb_wbssb_ssb> ssb_list = yb_ssbMapper.findAddress();

        //循环ssb_list得到ssbForeignId


        for (Iyqww_yb_wbssb_ssbVo ssbVo : ssbVoList) {
            //点位基础信息pointInformationMap
            Map<String, Object> pointInformationMap = new HashMap<>();

            String ssbVoForeignId = ssbVo.getForeignId();

            pointInformationMap.put("userName", ssbVo.getUserName());
            pointInformationMap.put("mobile", ssbVo.getMobile());
            pointInformationMap.put("address", ssbVo.getAddress());
            pointInformationMap.put("addressJwd", ssbVo.getAddressJwd());
            pointInformationMap.put("foreignId", ssbVo.getForeignId());
            pointInformationMap.put("deptName", ssbVo.getDeptName());
            pointInformationMap.put("completed", 0);
            pointInformationMap.put("processing", 0);


            for (Iyqww_yb_wbssb_ssb iy_ssb : ssb_list) {
<<<<<<< HEAD


=======


>>>>>>> 3f039bc935edf091c1f1437e0c1ddcf82f9a410e
                String ssbForeignId = iy_ssb.getForeignId();
                //判断随手报vo的foreignId是否与1w1m_tb中的foreignId相同
                if (ssbForeignId != null && ssbForeignId != "" && ssbForeignId.equals(ssbVoForeignId)) {
                    //相同则查询办结情况
                    List<Map<String, Object>> ssbList = yb_ssbMapper.staticData(ssbForeignId);

                    for (Map<String, Object> map1 : ssbList) {

                        System.out.println("map1==" + map1);
                        if (map1.get("sequenceStatus") != null && map1.get("sequenceStatus") != "" && map1.get("sequenceStatus").equals("COMPLETED")) {
                            Object num = map1.get("num");
//                            System.out.println(ssbForeignId + "==已办结111==" + num);
                            pointInformationMap.put("completed", num);
                            System.out.println("已办结数量===" + num);
                        } else if (map1.get("sequenceStatus") != null && map1.get("sequenceStatus") != "" && map1.get("sequenceStatus").equals("PROCESSING")) {
                            Object num = map1.get("num");
//                            System.out.println(ssbForeignId + "==处理中111==" + num);
                            pointInformationMap.put("processing", num);
                        }

                    }
                }

            }

            list1.add(pointInformationMap);
            //System.out.println(list1);

        }


        return list1;
    }

    @Override
    public void updateVoinfo() {

        String listForeignId;

        List<Iyqww_yb_wbssb_ssbVo> ssbVoList = yb_ssbMapper.selectAll();
        List<Sheet4> sheet4List = yb_ssbMapper.findAll();

        for (Iyqww_yb_wbssb_ssbVo list : ssbVoList) {

            listForeignId = list.getForeignId();

            for (Sheet4 slist : sheet4List) {

                String slistForeignId = slist.getForeignId();
                if (slistForeignId != null && slistForeignId != "" && listForeignId != null && listForeignId != "" && slistForeignId.equals(listForeignId)) {

                    String addressJwd = slist.getAddressJwd();
//                    System.out.println("slistForeignId==="+slistForeignId);
//                    System.out.println("addressJwd==="+addressJwd);
                    yb_ssbMapper.addSsbvo(addressJwd, slistForeignId);


                }
            }

        }
    }

//    @PostConstruct
    public ArrayList<Map<String, Object>> getAddressInfo() {



        ArrayList<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();

        List<Iyqww_yb_wbssb_1w1mtb> list = yb_1w1m_tbMapper.findEssential();

        // regEEx-- String规则
        String regEx = "[\n`~!@#$%^&*()+=|';'\\[\\].<>/?~！@#￥%……&*（）——+|【】‘；：”“’。 、？]";
        // 把规则编译成模式对象
        Pattern p = Pattern.compile(regEx);

        String deptName = null;
        String userName = null;
        String mobile = null;
        String aa = "";
        // url 根据用户id获取用户信息 云枢接口
        String url = "http://acl.iansn.com/api/api/organization/user/info";
        // dept_url 根据部门id获取部门信息 云枢接口
        String dept_url = "http://acl.iansn.com/api/api/organization/department/get_dept_info";
        try {
            String token = getTokenUtil.getToken();

            for (Iyqww_yb_wbssb_1w1mtb iy : list) {

                //点位基础信息pointInformationMap
                Map<String, Object> pointInformationMap = new HashMap<>();
                //获取地址
                String address = iy.getAddress();
                //将地址转为经纬度
                String addressStr = JwdUtil.getLonLat1(address);
//                System.out.println(addressStr);
                //获取1w1m_tb的foreignId
                String foreignId = iy.getForeignId();

                //获取部门id
                String StaffSelector1608775420787 = iy.getStaffSelector1608775420787();

                if (StaffSelector1608775420787 != null && StaffSelector1608775420787 != "") {

                    // 通过模式对象得到匹配器对象
                    Matcher ma = p.matcher(StaffSelector1608775420787);
                    String newStr = ma.replaceAll(aa).trim();
                    //将处理后的字符串放到map中
                    Map map = (Map) JSON.parse(newStr);
                    //从map中取得id的值
                    String deptId = (String) map.get("id");
                    //根据部门ID获取部门信息
                    String dept = HttpClientUtils.doGetDept(dept_url, token, deptId);
                    //处理得到的部门信息
                    JSONObject deptJson = JSONObject.fromObject(dept);
                    //从部门信息中获取data的值
                    String dataStr = deptJson.getString("data");
                    //处理得到的data值
                    JSONObject deptJson1 = JSONObject.fromObject(dataStr);
                    //从data值中获取到部门名称deptName
                    deptName = deptJson1.getString("name");

                }


//                System.out.println("deptName============" + deptName);


                //获取用户id
                String staffSelector1608775349969 = iy.getStaffSelector1608775349969();

                if (staffSelector1608775349969 != null && staffSelector1608775349969 != "") {
                    Matcher m = p.matcher(staffSelector1608775349969);
                    String newString = m.replaceAll(aa).trim();
                    Map maps = (Map) JSON.parse(newString);
                    String userId = (String) maps.get("id");
                    //根据用户ID获取姓名、电话
                    String inforSt = HttpClientUtils.doGetUser(url, token, userId);
                    JSONObject userJson = JSONObject.fromObject(inforSt);
                    String user_data = userJson.getString("data");
                    JSONObject userJson1 = JSONObject.fromObject(user_data);
                    userName = userJson1.getString("name");
                    mobile = userJson1.getString("mobile");
                }


//                System.out.println("userName============" + userName);
////                System.out.println("mobile============" + mobile);

                pointInformationMap.put("userName", userName);
                pointInformationMap.put("mobile", mobile);
                pointInformationMap.put("address", address);
                pointInformationMap.put("addressJwd", addressStr);
                pointInformationMap.put("foreignId", foreignId);
                pointInformationMap.put("deptName", deptName);

                System.out.println(foreignId);
                list1.add(pointInformationMap);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list1;
    }


}
